/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.message.dto.DocTypeDTO;
import com.fuyao.cloud.admin.api.entity.DocType;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.admin.service.DocTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文档配置
 *
 * @author liyue
 * @date 2022-04-24 15:47:04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/doctype")
@Api(value = "doctype", tags = "文档配置管理")
public class DocTypeController {

    private final DocTypeService docTypeService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param docType 文档配置
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('admin_doctype_view')")
    public R getDocTypePage(Page page, DocType docType) {
        return R.ok(docTypeService.page(page, Wrappers.query(docType)));
    }


    /**
     * 通过id查询文档配置
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('admin_doctype_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(docTypeService.getById(id));
    }

    /**
     * 新增文档配置
     *
     * @param docType 文档配置
     * @return R
     */
    @ApiOperation(value = "新增文档配置", notes = "新增文档配置")
    @SysLog("新增文档配置")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_doctype_add')")
    public R save(@RequestBody DocType docType) {
        return R.ok(docTypeService.save(docType));
    }

    /**
     * 修改文档配置
     *
     * @param docType 文档配置
     * @return R
     */
    @ApiOperation(value = "修改文档配置", notes = "修改文档配置")
    @SysLog("修改文档配置")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_doctype_edit')")
    public R updateById(@RequestBody DocType docType) {
        return R.ok(docTypeService.updateById(docType));
    }

    /**
     * 通过id删除文档配置
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除文档配置", notes = "通过id删除文档配置")
    @SysLog("通过id删除文档配置")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('admin_doctype_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(docTypeService.removeById(id));
    }


    /**
     * 导出excel 表格
     *
     * @param docType 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('admin_doctype_export')")
    public List<DocType> export(DocType docType) {
        return docTypeService.list(Wrappers.query(docType));
    }

    @ApiOperation(value = "获取所有文种类型集合", notes = "获取所有文种类型集合")
    @GetMapping("/getAllDocTypeOption")
    public R<List<DocTypeDTO>> getAllDocTypeOption() {
        return R.ok(this.docTypeService.getAllDocTypeOption(Wrappers.emptyWrapper()));
    }

    @ApiOperation(value = "获取所有文种类型集合(带条件参数)", notes = "获取所有文种类型集合(带条件参数)")
    @GetMapping("/getDocTypeOption")
    public R<List<DocTypeDTO>> getAllDocTypeOption(DocType docType) {
        return R.ok(this.docTypeService.getAllDocTypeOption(Wrappers.query(docType)));
    }

    @ApiOperation(value = "根据编码获取文种类型", notes = "根据编码获取文种类型")
    @GetMapping("/{code}")
    public R getDocTypeByCode(@PathVariable String code) {
        return R.ok(docTypeService.getDocTypeByCode(code));
    }
}
